function handles = arrayTracesAveraged(handles)
% arrayTracesAveraged
%
% Plots the set of averaged map traces as a map.
%
% Editing:
% gs june 2005 -- adapted from arrayTracesAsInputMap 
% -----------------------------------------------------------------

flipFlag = 0;

% data
newArray = handles.data.map.traceAvg.bsArray;
[traceLength numSites] = size(newArray);

[mapRows mapCols] = size(handles.data.map.mapActive.mapPattern);
mapPattern = reshape((1:numSites), mapRows, mapCols);

try
    sr = handles.data.acq.sampleRate;
catch
    sr = 10000; 
    disp('Using sample rate of 10K by default');
end

startTime = 1;
stopTime = traceLength;

showStart = round(handles.data.analysis.traceMapShowStart * sr);
showStop = round(handles.data.analysis.traceMapShowStop * sr);

array = newArray(startTime : stopTime, :);
[rows, cols] = size(array);
totalTime = (rows - 1) / sr; 
xTimeAxis = linspace(0, totalTime, rows)';
traceAxis = ( 1 : cols );

[sizeX, sizeY] = size(mapPattern);

yFactor = handles.data.analysis.traceMapYFactor;
scaleBarTxt = 'pA';

if flipFlag == 1
    yFactor = -yFactor;
end

offsetVector = yFactor * ( 0 : cols-1 );
offsetArray = repmat(offsetVector, rows, 1);
array = array - offsetArray;

% set up the figure -------------------------------------------------------------
x = .14; 
y = .11; 
w = .5; 
h = .8; 
handles.data.map.mapActive.hTraceMapFig = figure('Units', 'normalized', ...
    'Position', [x y w h], 'Name', 'arrayTracesAsMap', ...
    'NumberTitle', 'off', 'Color', 'w');

subplotRows = 1; subplotCols = sizeY; plotnum = 0;

for N = 1:sizeY
    startInd = (N-1)*sizeX + 1;
    endInd = N*sizeX;
    plotnum = plotnum+1;

    pos1 = 0.025 + (N - 1)*(0.96/sizeY);
    pos2 = 0.02;
    pos3 = 0.05;
    pos4 = 0.96;
    hsub(N) = axes('Position', [pos1 pos2 pos3 pos4]);

    h = plot(xTimeAxis(showStart:showStop), array(showStart:showStop,startInd:endInd));
    set(h, 'Color', 'k', 'Tag', 'trace');
    hold on;
    
    minval = min(mean(array(1 : 100, startInd : endInd)));
    maxval = max(mean(array(1 : 100, startInd : endInd)));
    tweakFactor = abs(maxval - minval)*0.05;
    yrange = [minval-tweakFactor maxval+tweakFactor];
    set(gca, 'YLim', yrange);
    set(gca, 'XLim', [(showStart-200)/sr (showStop+200)/sr]);
    xlabel('Seconds');
end
set(findobj(gcf, 'Type', 'axes'), 'Visible', 'off');

k = strfind(handles.data.map.mapActive.directory, '\');
titleStr = [handles.data.analysis.experimentName ', averaged traces'];
text('String', titleStr, 'Units', 'Normalized', 'Position', [0 1.005], ...
    'FontSize', 12, 'FontWeight', 'Bold', 'Parent', hsub(1), ...
    'Tag', 'singleTraceMap', 'Interpreter', 'none');

Y = mean(array(:,end))+yFactor/4;
hscalebar(1) = line([.1 .2], [Y Y]);
hscalebar(2) = line([.1 .1], [Y Y+yFactor/2]);
set(hscalebar, 'Color', 'k');
ht(1) = text(.12, Y+yFactor/6, '100 ms'); 
ht(2) = text(.12, Y+yFactor/3, [num2str(yFactor/2) ' ' scaleBarTxt]); 
set(ht, 'Color', 'k', 'FontSize', 8);
